package com.sise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.bean.CodeMsg;
import com.sise.entity.ReceiverInfo;
import com.sise.mapper.ReceiverInfoMapper;
import com.sise.service.IReceiverInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zj
 */
@Service
public class ReceiverInfoServiceImpl extends ServiceImpl<ReceiverInfoMapper, ReceiverInfo> implements IReceiverInfoService {

    @Resource
    private ReceiverInfoMapper receiverInfoMapper;

    /**
     * 默认地址，0：否，1：是
     * 将该用户原本的收获人信息表的is_default_address为1的字段改为0
     */
    private void updateIsDefaultAddressToNo(ReceiverInfo receiverInfo) {
        QueryWrapper<ReceiverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReceiverInfo::getIsDefaultAddress, 1)
                .eq(ReceiverInfo::getOpenId, receiverInfo.getOpenId());
        List<ReceiverInfo> receiverInfos = receiverInfoMapper.selectList(queryWrapper);
        if (!receiverInfos.isEmpty()) {
            for (ReceiverInfo oldReceiverInfo : receiverInfos) {
                UpdateWrapper<ReceiverInfo> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("receiver_info_id", oldReceiverInfo.getReceiverInfoId());
                updateWrapper.set("is_default_address", 0);
                receiverInfoMapper.update(oldReceiverInfo, updateWrapper);
            }
        }
    }

    @Override
    public CodeMsg saveReceiverInfo(ReceiverInfo receiverInfo) {
        if (receiverInfo.getOpenId() == null) {
            return new CodeMsg().setCode(400).setMsg("请登陆后再进行操作");
        }
        if (receiverInfo.getIsDefaultAddress().equals(1)) {
            updateIsDefaultAddressToNo(receiverInfo);
        }
        if (receiverInfoMapper.insert(receiverInfo) == 1) {
            return new CodeMsg().setCode(200).setMsg("添加成功");
        }
        return new CodeMsg().setCode(401).setMsg("添加失败");
    }

    @Override
    public CodeMsg deleteById(ReceiverInfo receiverInfoId) {
        if (receiverInfoId.getOpenId() == null) {
            return new CodeMsg().setCode(400).setMsg("请登陆后再进行操作");
        }
        ReceiverInfo receiverInfo = receiverInfoMapper.selectById(receiverInfoId.getReceiverInfoId());
        if (receiverInfo != null) {
            if (receiverInfoMapper.deleteById(receiverInfoId) == 1) {
                return new CodeMsg().setCode(200).setMsg("成功");
            }
            return new CodeMsg().setCode(401).setMsg("删除失败");
        } else {
            return new CodeMsg().setCode(400).setMsg("地址不存在");
        }
    }

    @Override
    public CodeMsg updateReceiverInfoById(ReceiverInfo receiverInfo) {
        if (receiverInfo.getOpenId() == null) {
            return new CodeMsg().setCode(400).setMsg("请登陆后再进行操作");
        }
        ReceiverInfo getReceiverInfo = receiverInfoMapper.selectById(receiverInfo.getReceiverInfoId());
        if (getReceiverInfo != null) {
            if (receiverInfo.getIsDefaultAddress().equals(1)) {
                updateIsDefaultAddressToNo(receiverInfo);
            }
            if (receiverInfoMapper.updateById(receiverInfo) == 1) {
                return new CodeMsg().setCode(200).setMsg("成功");
            }
            return new CodeMsg().setCode(401).setMsg("更新失败");
        } else {
            return new CodeMsg().setCode(400).setMsg("地址不存在");
        }
    }

    @Override
    public List<ReceiverInfo> findByOpenId(String openId) {
        QueryWrapper<ReceiverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", openId);
        return receiverInfoMapper.selectList(queryWrapper);
    }

    @Override
    public ReceiverInfo findDefaultByOpenId(String openId) {
        QueryWrapper<ReceiverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", openId);
        queryWrapper.eq("is_default_address", 1);
        List<ReceiverInfo> receiverInfoList = receiverInfoMapper.selectList(queryWrapper);
        if (!receiverInfoList.isEmpty()) {
            return receiverInfoList.get(0);
        } else {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("open_id", openId);
            queryWrapper.eq("is_default_address", 0);
            List<ReceiverInfo> receiverInfos = receiverInfoMapper.selectList(queryWrapper);
            if (!receiverInfos.isEmpty()) {
                return receiverInfos.get(0);
            }
            return null;
        }
    }
}
